﻿using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using FDEEPublicFunc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FDEEInterface
{
    public static class PileRebarDrawing
    {
      
        /// <summary>
        /// 环形阵列
        /// </summary>
        /// <param name="ent">图形对象</param>
        /// <param name="num">图形数量</param>
        /// <param name="degree">中心点到各个图形的夹角</param>
        /// <param name="center">中心点</param>
        /// <returns>List</returns>
        public static List<Entity> ArrayPolarEntity(this Entity ent, int num, double degree, Point3d center, string layer = "细线")
        {
            // 声明一个List集合 用于返回
            List<Entity> entList = new List<Entity>();
            if (ent.IsNewObject == true)
            {

                Database db = HostApplicationServices.WorkingDatabase;
                using (Transaction trans = db.TransactionManager.StartTransaction())
                {
                    // 打开块表
                    BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
                    // 打开块表记录
                    BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
                    LayerTable lt = trans.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                    degree = degree > 360 ? 360 : degree;
                    degree = degree < -360 ? -360 : degree;
                    int divAngnum = num - 1;
                    if (degree == 360 || degree == -360)
                    {
                        divAngnum = num;
                    }
                    for (int i = 0; i < num; i++)
                    {
                        Matrix3d mt = Matrix3d.Rotation(GeTools.DegreeToRadian(i * degree / divAngnum), Vector3d.ZAxis, center);
                        Entity entA = ent.GetTransformedCopy(mt);
                        entA.LayerId = lt[layer];
                        Entity entB = entA.Clone() as Entity;
                        entList.Add(entB);
                        btr.AppendEntity(entA);
                        trans.AddNewlyCreatedDBObject(entA, true);

                    }
                    trans.Commit();
                }

            }
            return entList;
        }


        public static List<Entity> ArrayPolarEntity(this Entity ent, int num, double degree, Point3d center, List<int> hL, List<int> hNV, out List<Entity> hEnt, out List<Entity> nhEnt, string layer = "细线")
        {
            hEnt = new List<Entity>();
            nhEnt= new List<Entity>();
            // 声明一个List集合 用于返回
            List<Entity> entList = new List<Entity>();
            if (ent.IsNewObject == true)
            {

                Database db = HostApplicationServices.WorkingDatabase;
                using (Transaction trans = db.TransactionManager.StartTransaction())
                {
                    // 打开块表
                    BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
                    // 打开块表记录
                    BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
                    LayerTable lt = trans.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                    degree = degree > 360 ? 360 : degree;
                    degree = degree < -360 ? -360 : degree;
                    int divAngnum = num - 1;
                    if (degree == 360 || degree == -360)
                    {
                        divAngnum = num;
                    }
                    for (int i = 0; i < num; i++)
                    {
                        if (hNV.Count > 0)
                        {
                            if (hNV.Contains(i))
                            {
                                continue;
                            }
                        }
                        Matrix3d mt = Matrix3d.Rotation(GeTools.DegreeToRadian(i * degree / divAngnum), Vector3d.ZAxis, center);
                        Entity entA = ent.GetTransformedCopy(mt);
                        entA.LayerId = lt[layer];
                        Entity entB = entA.Clone() as Entity;
                        entList.Add(entB);
                      
                        btr.AppendEntity(entA);
                        trans.AddNewlyCreatedDBObject(entA, true);
                        if (hL.Contains(i))
                        {
                            HatchPloter.HatchEnity(db, "SOLID", 0.04, 0, entA.ObjectId);
                            hEnt.Add(entB);
                        }
                        else
                        {
                            nhEnt.Add(entB);
                        }
                    }
                    trans.Commit();
                }

            }
            return entList;
        }

        //hatchNV = new 
    }
}
